import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/books.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/routes/routePages.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/layouts/appdrawerTeacher.dart';
import 'package:ichinesereader/widgets/layouts/customAppBar.dart';
import 'package:ichinesereader/widgets/tabs/textbookTab.dart';
import '../../themes/pageStyles.dart';
import '../../widgets/sidebars/teacherSidebar.dart';

class TextBookForTeacherPage extends StatefulWidget {
  static const String routeName = '/teacher/corematerials';

  @override
  State<TextBookForTeacherPage> createState() => _TextBookForTeacherPageState();
}

class _TextBookForTeacherPageState extends State<TextBookForTeacherPage> {
  final box = Hive.box('appconfig');
  final BooksController _booksController = Get.find();
  bool isTeacher = false;

  @override
  void initState() {
    isTeacher = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_Teacher;
    String? _programId =
        box.get(CacheManagerKey['currentclassProgramId'].toString());

    if (isTeacher) {
      _booksController.fetchBookAllFolders();
      if (_programId != null && _programId != '')
        _booksController.fetchProgramBookList(_programId);
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build TextBookForTeacherPage');
    final isSmallScreen =
        MediaQuery.of(context).size.width < Constants.DefinedScreenSmallSize;
    final isBigScreen =
        MediaQuery.of(context).size.width > Constants.DefinedScreenBigSize;
    int _selectedSidebarItem = RoutePages.teacherPageItems.indexWhere((page) =>
        page.route == '/teacher/themedbooks' ||
        page.route =='/teacher/corematerials');
    final PageStyles _pageStyles = PageStyles();

    return Scaffold(
      appBar: isSmallScreen ? CustomAppBar(title: 'Core Materials'.tr) : null,
      drawer: isSmallScreen ? TeacherAppDrawer() : null,
      body: Container(
        decoration: _pageStyles.pageBoxDecoration(),
        child: Row(
          children: [
            Visibility(
                visible: !isSmallScreen,
                child: TeacherSidebarX(
                  selectedIndex: _selectedSidebarItem,
                  isBigScreen: isBigScreen,
                )),
            Expanded(
              child: isTeacher ? TextbookTabController() : Text(''),
            ),
          ],
        ),
      ),
    );
  }
}
